/*
 +-----------------------------------------------------------------------------------------------+
 | Project : UKAPI
 +-----------------------------------------------------------------------------------------------+
 | Project Description : Universal & Kernel API
 +-----------------------------------------------------------------------------------------------+
 | File Description :
 |   Device Manager
 |                                                 
 +-----------------------------------------------------------------------------------------------+
 | Licence : MIT Licence
 +-----------------------------------------------------------------------------------------------+
 | Copyright (c) 2006 Olivier Vanhoucke
 | 
 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 | and associated documentation files (the "Software"), to deal in the Software without 
 | restriction, including without limitation the rights to use, copy, modify, merge, publish, 
 | distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 
 | Software is furnished to do so, subject to the following conditions:
 | 
 | The above copyright notice and this permission notice shall be included in all copies or 
 | substantial portions of the Software.
 | 
 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 | BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 +-----------------------------------------------------------------------------------------------+
*/


/**
 * Inclusions standards
 */
#include <ukapi.h>

result_t device_manager_task( pointer_t arg )
{
	result_t res;

	do
	{
		pmessage_t			pmsg	= null;
		pdevice_request_t	pdr		= null;
		
		res = device_request_wait( &pmsg );

		if ( result_failed( res ) )
		{
			error_message( error_zone_unknown, ( "[device_request_wait()]" ) );
			continue;
		}

		pdr = (pdevice_request_t) pmsg->data;

		// try catch ?

		switch ( pdr->request_type )
		{
			case device_request_type_read_e:
				pdr->stream_driver->read( pdr->stream_driver, pdr->buffer_in );
				break;

			case device_request_type_write_e:
				pdr->stream_driver->write( pdr->stream_driver, pdr->buffer_out );
				break;

			case device_request_type_seek_e:
				pdr->stream_driver->seek( pdr->stream_driver );
				break;

			case device_request_type_iocontrol_e:
				pdr->stream_driver->iocontrol( pdr->stream_driver, pdr->buffer_in, pdr->buffer_out );
				break;

			case device_request_type_power_on_e:
				pdr->stream_driver->power_on( pdr->stream_driver );
				break;

			case device_request_type_power_off_e:
				pdr->stream_driver->power_off( pdr->stream_driver );
				break;

			case device_request_type_open_e:
				pdr->stream_driver->open( pdr->stream_driver );
				break;

			case device_request_type_close_e:
				pdr->stream_driver->close( pdr->stream_driver );
				break;

			default:
				break;
		}

		message_destroy( pmsg );

	} while ( true ); // TODO : remove infinite loop !!!!

	return result_ok_e;
}
